//
//  SearchViewController.m
//  A Learning Walk
//
//  Created by dllo on 16/6/25.
//  Copyright © 2016年 战斗民族. All rights reserved.
//

#import "SearchViewController.h"

#import <AMapSearchKit/AMapSearchKit.h>

@interface SearchViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)UIView *viewT;
@property(nonatomic, retain)UITextField *myText;
@property(nonatomic, retain)UILabel *labelS;
@property(nonatomic, retain)UITableView *myTable;
@property(nonatomic, retain)NSMutableArray *myArr;

@property(nonatomic, retain)MAPointAnnotation *annotation;

@end

@implementation SearchViewController

-(void)dealloc{
    [_viewT release];
    [_myText release];
    [_labelS release];
    [_myTable release];
    [_myArr release];
    [_annotation release];
    [_pois1 release];

    [super dealloc];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];


    self.myArr = [NSMutableArray arrayWithArray:self.pois1];
 
    [self initView];
    [self creatTable];
}


-(void)initView{
    
    self.viewT = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 60)];
    self.viewT.backgroundColor = [UIColor colorWithRed:202/255.0 green:227/255.0 blue:232/255.0 alpha:1];
    [self.view addSubview:self.viewT];
    
//收索框
    self.myText = [[UITextField alloc] initWithFrame:CGRectMake(40, 30, self.view.frame.size.width-120, 23)];
    self.myText.backgroundColor = [UIColor colorWithRed:165/255.0 green:178/255.0 blue:170/255.0 alpha:1];
    [self.viewT addSubview:self.myText];
    self.myText.font = [UIFont systemFontOfSize:15];
    self.myText.placeholder = @"  收索或输入地址" ;
    self.myText.borderStyle = UITextBorderStyleRoundedRect;
    
//搜索按钮
    self.labelS = [[UILabel alloc] initWithFrame:CGRectMake(self.myText.frame.size.width+self.myText.frame.origin.x+20, 30, 40, 20)];
    self.labelS.backgroundColor = [UIColor clearColor];
    self.labelS.text = @"搜索";
    self.labelS.font = [UIFont systemFontOfSize:15];
    [self.viewT addSubview:self.labelS];
    self.labelS.userInteractionEnabled = YES;
    //手势
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGes:)];
    [self.labelS addGestureRecognizer:tapGes];
    
    [self.viewT release];
    [self.labelS release];
    [self.myText release];
}

-(void)creatTable{
    self.myTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height-60) style:UITableViewStylePlain];
    self.myTable.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myTable];
    self.myTable.delegate = self;
    self.myTable.dataSource = self;
    [self.myTable registerClass:[PlaceTableViewCell class] forCellReuseIdentifier:@"cellw"];
    
    [self.myTable release];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PlaceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellw" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[PlaceTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                      reuseIdentifier:@"cellw"];
    }
    AMapPOI *poi = [self.myArr objectAtIndex:indexPath.row];
    cell.textLabel.text = poi.name;
//    cell.detailTextLabel.text = poi.address;
    
    return cell;
}
//Cell个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.myArr.count;
}
//tableViewCell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
//搜索按钮手势
-(void)tapGes:(UITapGestureRecognizer *)noti{

    [self dismissViewControllerAnimated:YES completion:^{
        
    }];

}

//tableView的点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //为点击的poi点添加标注
    AMapPOI *Poi = _myArr[indexPath.row];
    self.annotation = [[MAPointAnnotation alloc] init];
    self.annotation.coordinate = CLLocationCoordinate2DMake(Poi.location.latitude, Poi.location.longitude);
    self.annotation.title = Poi.name;
    self.annotation.subtitle = Poi.address;
    
#warning 发送通知
    //将得到的点的位置传回,显示在地图上
    [[NSNotificationCenter defaultCenter] postNotificationName:@"chuanzhi" object:self.annotation];
    //返回
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}


//在MAMapView的回调函数中,取得或创建annotationView,并配置annotationView的属性
-(MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation{
    static NSString *annotationIndetifier = @"annotationIndetifier";
    
    if ([annotation isKindOfClass:[MAPointAnnotation class]]) {
        MAPinAnnotationView *annotationView = (MAPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationIndetifier];
        if (annotationView == nil) {
            annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIndetifier];
        }
        annotationView.canShowCallout = YES;
        annotationView.animatesDrop = YES;
        return annotationView;
    }
    return nil;
}

//重写接收到的数组的set方法
-(void)setPois1:(NSMutableArray *)pois1{

    _pois1 = pois1;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
